<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="" prop="id">
                    <el-input v-model="formData.id" placeholder="请输入" />
                </el-form-item>
                <el-form-item label="标题" prop="title">
                    <el-input
                        v-model="formData.title"
                        placeholder="请输入标题"
                        type="textarea"
                        :autosize="{ minRows: 4, maxRows: 6 }"
                    />
                </el-form-item>
                <el-form-item label="文章内容" prop="content">
                    <editor v-model="formData.content" :height="500" />
                </el-form-item>
                <el-form-item label="1公告 2文章" prop="type">
                    <el-select class="flex-1" v-model="formData.type" placeholder="请选择1公告 2文章">
                        <el-option label="请选择字典生成" value="" />
                    </el-select>
                </el-form-item>
                <el-form-item label="文章主图" prop="mainPic">
                    <el-input
                        v-model="formData.mainPic"
                        placeholder="请输入文章主图"
                        type="textarea"
                        :autosize="{ minRows: 4, maxRows: 6 }"
                    />
                </el-form-item>
                <el-form-item label="文章主图缩略图" prop="mainPicThumb">
                    <el-input
                        v-model="formData.mainPicThumb"
                        placeholder="请输入文章主图缩略图"
                        type="textarea"
                        :autosize="{ minRows: 4, maxRows: 6 }"
                    />
                </el-form-item>
                <el-form-item label="删除时间" prop="deletedAt">
                    <el-date-picker
                        class="flex-1 !flex"
                        v-model="formData.deletedAt"
                        type="datetime"
                        clearable
                        value-format="YYYY-MM-DD hh:mm:ss"
                        placeholder="请选择删除时间"
                    />
                </el-form-item>
                <el-form-item label="创建人" prop="createdBy">
                    <el-input v-model="formData.createdBy" placeholder="请输入创建人" />
                </el-form-item>
                <el-form-item label="创建时间" prop="createdAt">
                    <el-date-picker
                        class="flex-1 !flex"
                        v-model="formData.createdAt"
                        type="datetime"
                        clearable
                        value-format="YYYY-MM-DD hh:mm:ss"
                        placeholder="请选择创建时间"
                    />
                </el-form-item>
                <el-form-item label="是否展示" prop="isShow">
                    <el-input v-model="formData.isShow" placeholder="请输入是否展示" />
                </el-form-item>
                <el-form-item label="" prop="sort">
                    <el-input v-model="formData.sort" placeholder="请输入" />
                </el-form-item>
                <el-form-item label="内容概述" prop="desc">
                    <el-input v-model="formData.desc" placeholder="请输入内容概述" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {  articleEdit, articleAdd, articleDetail } from '@/api/article'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑文章公告' : '新增文章公告'
})

const formData = reactive({
    id: '',
    title: '',
    content: '',
    type: '',
    mainPic: '',
    mainPicThumb: '',
    deletedAt: '',
    createdBy: '',
    createdAt: '',
    isShow: '',
    sort: '',
    desc: '',
})

const formRules = {
    id: [
        {
            required: true,
            message: '请输入',
            trigger: ['blur']
        }
    ],
    title: [
        {
            required: true,
            message: '请输入标题',
            trigger: ['blur']
        }
    ],
    content: [
        {
            required: true,
            message: '请输入文章内容',
            trigger: ['blur']
        }
    ],
    type: [
        {
            required: true,
            message: '请选择1公告 2文章',
            trigger: ['blur']
        }
    ],
    mainPic: [
        {
            required: true,
            message: '请输入文章主图',
            trigger: ['blur']
        }
    ],
    mainPicThumb: [
        {
            required: true,
            message: '请输入文章主图缩略图',
            trigger: ['blur']
        }
    ],
    deletedAt: [
        {
            required: true,
            message: '请选择删除时间',
            trigger: ['blur']
        }
    ],
    createdBy: [
        {
            required: true,
            message: '请输入创建人',
            trigger: ['blur']
        }
    ],
    createdAt: [
        {
            required: true,
            message: '请选择创建时间',
            trigger: ['blur']
        }
    ],
    isShow: [
        {
            required: true,
            message: '请输入是否展示',
            trigger: ['blur']
        }
    ],
    sort: [
        {
            required: true,
            message: '请输入',
            trigger: ['blur']
        }
    ],
    desc: [
        {
            required: true,
            message: '请输入内容概述',
            trigger: ['blur']
        }
    ],
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await articleEdit(data) : await articleAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await articleDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
